#include "voice.h"
#include <string.h>
#include "printf.h"
#include <stdarg.h>
#include "C_code_Log.h"
#include "color.h"
#include "jq8400.h"
#ifdef ZH
static uint8_t Voice_SeekNumber(uint8_t number);
static uint8_t Voice_SeekLetter(char letter);
static uint8_t Voice_SeekColor(Color color);
/**
 * @brief 发送数据，进行语音播报
 * 
 * @param data 要播报的内容
 */
void Voice_SendData(enum VoiceType type,...)
{
    va_list args;
    va_start(args,type);
    switch (type) {
      case VOICE_Platform: {
        int platform = va_arg(args, int);
        LOG_INFO("到达平台%d", platform);
        // 后续操作
        uint8_t Data[] = {VOICE_ARRIVE,Voice_SeekNumber(platform),VOICE_PLATFORM};
        JQ8400_CombinePlayingMusic(Data,3);
      } break;
      case VOICE_Upright: {
        LOG_INFO("到达直立景点");
        // 后续操作
        uint8_t Data[] = {VOICE_UPRIGHT};
        JQ8400_CombinePlayingMusic(Data,1);
      } break;
      case VOICE_Clue: {
        char clueIndex = va_arg(args, int);
        int clueData = va_arg(args, int);
        LOG_INFO("线索%c为%d", clueIndex, clueData);
        // 后续操作
        uint8_t Data[] = {VOICE_CLUE,Voice_SeekLetter(clueIndex),VOICE_ISIS,Voice_SeekNumber(clueData)};
        JQ8400_CombinePlayingMusic(Data,4);
      } break;
      case VOICE_Treasure: {
        LOG_INFO("发现宝物");
        // 后续操作
        uint8_t Data[] = {VOICE_FIND_TREASURE};
        JQ8400_CombinePlayingMusic(Data,1);
      } break;
      case VOICE_Debug: {
        int debugType = va_arg(args, int);
        if (debugType == 1) {  // 红绿灯调试
          Color color = va_arg(args, Color);
          LOG_INFO("红绿灯调试，颜色为%d", color);
          // 后续操作
          uint8_t Data[] = {Voice_SeekColor(color)};
          JQ8400_CombinePlayingMusic(Data,1);
        }
      } break;
      case VOICE_Platform_1: {
        LOG_INFO("准备完毕");
        uint8_t Data[] = {VOICE_READY};
        JQ8400_CombinePlayingMusic(Data,1);
        break;
      }
      default:
        break;
    }
    va_end(args);
}
#endif
/**
 * @brief 调节语音播报的音量大小
 * 
 * @param volume 设置的音量大小(1-4)，默认最大
 */
void Voice_SetVolume(uint8_t volume)
{
    char str[6];
    sprintf(str,"<Z>%d",volume);
    HAL_UART_Transmit(&VOICE_UART,(uint8_t *)str,strlen(str),0x100);
}

/**
 * @brief 调节语音播报的语速
 * 
 * @param speed 设置的播报语速(1-3)，默认为2
 */
void Voice_SetSpeed(uint8_t speed)
{
    char str[6];
    sprintf(str,"<S>%d",speed);
    HAL_UART_Transmit(&VOICE_UART,(uint8_t *)str,strlen(str),0x100);
}

/**
 * @brief 设置上电提示音，默认开启
 * 
 * @param tip true表示开启提示音，false表示关闭提示音
 */
void Voice_SetPowerTip(bool tip)
{
    if(tip)
    {
        char str[6] = "<I>1";
        HAL_UART_Transmit(&VOICE_UART,(uint8_t *)str,strlen(str),0x100);
    }
    else
    {
        char str[6] = "<I>0";
        HAL_UART_Transmit(&VOICE_UART,(uint8_t *)str,strlen(str),0x100);
    }
}
#ifdef ZH
static uint8_t Voice_SeekNumber(uint8_t number)
{
    switch (number)
    {
    case 0:
        return VOICE_NUMBER_0;
        break;
    case 1:
        return VOICE_NUMBER_1;
        break;
    case 2:
        return VOICE_NUMBER_2;
        break;
    case 3:
        return VOICE_NUMBER_3;
        break;
    case 4:
        return VOICE_NUMBER_4;
        break;
    case 5:
        return VOICE_NUMBER_5;
        break;
    case 6:
        return VOICE_NUMBER_6;
        break;
    case 7:
        return VOICE_NUMBER_7;
        break;
    case 8:
        return VOICE_NUMBER_8;
        break;
    case 9:
        return VOICE_NUMBER_9;
        break;
    default:
        return VOICE_NUMBER_9;
        break;
    }
}

static uint8_t Voice_SeekLetter(char letter)
{
    switch (letter)
    {
    case 'A':
        return VOICE_LETTER_A;
        break;
    case 'B':
        return VOICE_LETTER_B;
        break;
    case 'C':
        return VOICE_LETTER_C;
        break;
    
    default:
        return VOICE_LETTER_A;
        break;
    }
}

static uint8_t Voice_SeekColor(Color color)
{
    switch (color)
    {
    case COLOR_RED:
        return VOICE_RED;
        break;
    case COLOR_YELLOW:
        return VOICE_YELLOW;
        break;
    case COLOR_GREEN:
        return VOICE_GREEN;
        break;
    
    default:
        return VOICE_RED;
        break;
    }
}
#endif